﻿using SysController;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ADSystem
{
    public partial class frmProductInfo : Form
    {
        public frmProductInfo()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            dgvProductInfo.DataSource = ProductController.QueryProductInfo();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            MODEL.ProductInfo model = new MODEL.ProductInfo()
            {
                Code = this.txtCode.Text.Trim(),
                ProductDesc = this.txtDesc.Text.Trim(),
                ProductPackage = this.txtPackage.Text.Trim(),
                ProductName = this.txtProductName.Text.Trim(),
                ProductPrice = Convert.ToDecimal(this.txtProductPrice.Text.Trim()),
                ProductStatusID = Convert.ToInt16(this.txtStatusID.Text.Trim()),
                Remark = this.txtReamk.Text.Trim(),
                IsDel = this.Del.Checked,
                IsLock = this.Lock.Checked,
                CreateTime = DateTime.Now,
                CreateUser = "",
                UpdateTime = DateTime.Now,
                UpdateUser = ""
            };
            int row = ProductController.SaveProductInfo(model);
            MessageBox.Show(string.Format("操作执行{0} 条数据", row));
        }

        private void dgvProductInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            if (e.ColumnIndex == -1) return;
            if (dgvProductInfo.Columns[e.ColumnIndex].Name == "cSelect")
            {
                dgvProductInfo.CurrentCell.Value = !Convert.ToBoolean(dgvProductInfo.CurrentCell.Value);
            }

            DataGridViewRow dr = this.dgvProductInfo.CurrentRow;
            loadToControl(dr);
        }

        private void loadToControl(DataGridViewRow dr)
        {
            this.txtCode.Text = (dr.Cells["cCode"].Value ?? "").ToString();
            this.txtProductName.Text = (dr.Cells["cProductName"].Value ?? "").ToString();
            this.txtDesc.Text = (dr.Cells["cProductDesc"].Value ?? "").ToString();
            this.txtPackage.Text = (dr.Cells["cProductPackage"].Value ?? "").ToString();
            this.txtProductPrice.Text = dr.Cells["cProductPrice"].Value.ToString();
            this.txtReamk.Text = dr.Cells["cRemark"].Value.ToString();
            this.txtStatusID.Text = dr.Cells["cProductStatusID"].Value.ToString();
        }
    }
}
